/**
 * @Author Roy
 * @Description 初始化数据库连接池
 * @File:  Mysql.go
 * @CreatDate 2022/2/5 12:07
**/
package dao

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"time"
)


var(
	Db *gorm.DB
	err error
)

func InitMySQL(){
	dsn := "root:bytedancecamp@tcp(180.184.70.56:3306)/selectcourse?charset=utf8mb4&parseTime=true&loc=Local"
	Db,err = gorm.Open("mysql",dsn)
	if err != nil {
		fmt.Println("连接数据库失败，请检查参数：", err)
		return
	}
	Db.LogMode(true) //显示日志
	Db.DB().SetMaxIdleConns(10) //配置连接池
	Db.DB().SetMaxOpenConns(100)
	Db.DB().SetConnMaxLifetime(10*time.Second)
	Db.SingularTable(true)
}

func Close(){
	Db.Close()
}